<template>
  <div class="wrapper">
      <el-form :inline="true">
        <el-form-item>
          <date-phase ref="datePhase" dateTypeRange="234" :datePhase="datePhase" ></date-phase>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="searchItem()">查询</el-button>
          <el-button type="primary" @click="exportExcel()">导出</el-button>
        </el-form-item>
      </el-form>

      <div>
        <el-table ref="table" :height="tableHeight" :data="tableData"  border style="width: 100%;overflow-x: auto;" >
          <el-table-column label="时间" prop="date" min-width="160" align="center"></el-table-column>
          <el-table-column label="平台" prop="name" min-width="180" align="center"></el-table-column>
          <el-table-column label="访客数" prop="visitNum" min-width="100" align="center">
            <template slot-scope="scope">
              {{scope.row.visitNum? scope.row.visitNum: '0'}}
            </template>
          </el-table-column>
          <el-table-column label="业绩" prop="saleValue" min-width="100" align="center">
            <template slot-scope="scope">
              {{scope.row.saleValue? scope.row.saleValue: '0'}}
            </template>
          </el-table-column>
          <el-table-column label="业绩(不含邮费)" prop="saleValueWithoutPostage" min-width="120" align="center">
            <template slot-scope="scope">
              {{scope.row.saleValueWithoutPostage? scope.row.saleValueWithoutPostage: '0'}}
            </template>
          </el-table-column>
          <el-table-column label="货款金额" prop="goodsTotal" min-width="100" align="center">
            <template slot-scope="scope">
              {{scope.row.goodsTotal? scope.row.goodsTotal: '0'}}
            </template>
          </el-table-column>
          <el-table-column label="客单价" prop="valuePerOrder" min-width="100" align="center">
            <template slot-scope="scope">
              {{scope.row.valuePerOrder? scope.row.valuePerOrder: '0'}}
            </template>
          </el-table-column>
          <el-table-column label="转化率 %" prop="transferRate" min-width="100" align="center">
            <template slot-scope="scope">
              {{scope.row.transferRate? scope.row.transferRate: '0'}}
            </template>
          </el-table-column>
          <el-table-column label="zigbee销售额" prop="zigbeeSaleValue" min-width="100" align="center">
            <template slot-scope="scope">
              {{scope.row.zigbeeSaleValue? scope.row.zigbeeSaleValue: '0'}}
            </template>
          </el-table-column>
          <el-table-column label="zigbee销量" prop="zigbeeSaleCount" min-width="100" align="center">
            <template slot-scope="scope">
              {{scope.row.zigbeeSaleCount? scope.row.zigbeeSaleCount: '0'}}
            </template>
          </el-table-column>
          <el-table-column label="平台服务分" prop="platServeScores" min-width="100" align="center">
            <template slot-scope="scope">
              {{scope.row.platServeScores? scope.row.platServeScores: '0'}}
            </template>
          </el-table-column>
          <el-table-column label="广告费" prop="adCost" min-width="100" align="center">
            <template slot-scope="scope">
              {{scope.row.adCost? scope.row.adCost: '0'}}
            </template>
          </el-table-column>
          <el-table-column label="ACOS%" prop="acos" min-width="100" align="center">
            <template slot-scope="scope">
              {{scope.row.acos? scope.row.acos + '%': '0%'}}
            </template>
          </el-table-column>
          <el-table-column label="营销活动支出" prop="discount" min-width="120" align="center">
            <template slot-scope="scope">
              {{scope.row.discount? scope.row.discount + '': '0'}}
            </template>
          </el-table-column>
          <el-table-column label="营销活动占比%" prop="discountPercent" min-width="120" align="center">
            <template slot-scope="scope">
              {{scope.row.discountPercent? scope.row.discountPercent + '%': '0%'}}
            </template>
          </el-table-column>
        </el-table>
      </div>
  </div>
</template>

<script>
  import dingMixin from  '@/mixins/ding'
  import formUtil from '@/utils/formUtil'
  import DatePhase from  '@/components/sale/date-phase'
  import FileSaver from 'file-saver'
  import XLSX from 'xlsx'

  export default {
    data () {
      return {
        countryData: [],
        tableData: [],
        defaultProps: {
          children: 'children',
          label: 'name',
          isLeaf: 'leaf'
        },
        datePhase:{
          dateType: 2,
          value:'',
          to:''
        },
        platId: '',
        name: '',
        tableHeight: window.tableHeight,
        treeData: []
      }
    },
    components: {
      DatePhase
    },
    mixins: [dingMixin],
    activated() {
      this.tableHeight = document.documentElement['clientHeight'] - 220
      window.onresize = () => {
        this.tableHeight = document.documentElement['clientHeight'] - 220
      }
    },
    async mounted(){
      this.platId = this.$route.query.platId;
      this.name = this.$route.query.name;
      this.datePhase.to = '';
      this.datePhase.value = '';
      this.searchItem();
    },
    methods: {
      async searchItem() {
        this.tableData = await this.$httpN.postB('mpb/report/plat',{
          from: this.datePhase.value,
          to: this.datePhase.to,
          dateType: this.datePhase.dateType,
          platId: this.platId,
        })
      },
      exportExcel(){
        this.$nextTick(function () {
          let wb = XLSX.utils.table_to_book(this.$refs.table.$el);
          let wbout = XLSX.write(wb, { bookType: 'xlsx', bookSST: true, type: 'array' });
          let name = this.name ? this.name: ''
          try {
            FileSaver.saveAs(new Blob([wbout], { type: 'application/octet-stream' }), `${name}平台运营报表.xlsx`)
          } catch (e) {
            if (typeof console !== 'undefined') console.log(e, wbout)
          }
          return wbout;
        });
      }
    }
  }
</script>

<style lang="scss" scoped>

</style>
